﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace seo.util
{
    public class HtmlAnalyzer
    {
        int articleLength = 300;
        public string deleteHtml(string html)
        {
            System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+?</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+?</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+?</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+?\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*?>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex0 = new System.Text.RegularExpressions.Regex(@"\n+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex10 = new System.Text.RegularExpressions.Regex(@"<style[\s\S]+?</style *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex11 = new System.Text.RegularExpressions.Regex(@"(\s+\n)+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记
            html = regex10.Replace(html, "");
            //html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
            //html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
            html = regex4.Replace(html, ""); //过滤iframe
            html = regex5.Replace(html, ""); //过滤frameset
            //html = regex6.Replace(html, ""); //过滤frameset
            html = regex7.Replace(html, ""); //过滤frameset
            html = regex8.Replace(html, ""); //过滤frameset
            html = regex9.Replace(html, "");
            //html = html.Replace(" ", "");
            html = regex11.Replace(html, "\n");
            html = html.Replace("\t", "");
            html = regex0.Replace(html, "\n");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            return html;
        }

        public string getUrlsFromHtml(string html)
        {
            string result = "";
            Regex r = new Regex(@"(<h3 class=""r"">[\s\S]+?</h3>)", RegexOptions.IgnoreCase);
            Match m = r.Match(html);
            //int matchCount = 0;
            while (m.Success)
            {
                result += m.Value + "\n";
                m = m.NextMatch();
            }
            return result;
        }

        public string getUrlString(string html)
        {
            string result = string.Empty;
            //string regex = "href=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\']";
            string regex = @"href=\""((?<url>[\s\S]*?))\""";
            Regex r = new Regex(regex, RegexOptions.IgnoreCase);
            Match m = r.Match(html);
            //int matchCount = 0;
            while (m.Success)
            {
                result += m.Groups[1].Value + ";\n";
                m = m.NextMatch();
            }

            return result;
        }

        public List<string> getUrlList(string html)
        {
            List<string> list = new List<string>();
            html = getUrlsFromHtml(html);
            //string regex = "href=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\']";
            //string regex = @"href=[""'](http://[^\*""]+?[^/])[""']";

            string regex = @"href=\""((?<url>[\s\S]*?))\""";
            Regex r = new Regex(regex, RegexOptions.IgnoreCase);
            Match m = r.Match(html);
            //int matchCount = 0;
            while (m.Success)
            {
                list.Add(m.Groups[1].Value);
                m = m.NextMatch();
            }

            return list;
        }

        public string getArticleFromHtml(string html, string keyword)
        {
            string result = string.Empty;
            bool f = false;
            string regex = @"<td.*?>(.+?)</td *>";
            Regex r = new Regex(regex, RegexOptions.IgnoreCase);
            Match m = r.Match(html);
            while (m.Success)
            {
                if (m.Groups[1].Value.Contains(keyword) && m.Groups[1].Value.Length > articleLength)
                {
                    result = m.Groups[1].Value;
                    f = true;
                    break;
                }
                m = m.NextMatch();
            }
            if (f) return result;
            //regex = @"<div.*?>(.+?)</div *>";
            regex = "<div[^>]*>.*(((?'Open'<div[^>]*>)[^<>]*)+((?'-Open'</div>)[^<>]*)+)*(?(Open)(?!))</div>";
            r = new Regex(regex, RegexOptions.IgnoreCase);
            m = r.Match(html);
            while (m.Success)
            {
                if (m.Value.Contains(keyword) && m.Value.Length > articleLength)
                {
                    result = m.Value;
                    f = true;
                    break;
                }
                m = m.NextMatch();
            }
            return result;
        }

        public string getArticleFromHtmlTest(string html, string keyword)
        {
            string result = string.Empty;
            string regex = @"\b((?!abc)\w)+\b";
            Regex r = new Regex(regex, RegexOptions.IgnoreCase);
            Match m = r.Match(html);
            while (m.Success)
            {
                result = m.Value;
                break;
                m = m.NextMatch();
            }
            return result;
        }
    }
}
